package org.mcjug.locationmailer.service;

import java.util.Date;

import org.mcjug.locationmailer.R;
import org.mcjug.locationmailer.db.StatusMessageDatabaseHelper;
import org.mcjug.locationmailer.model.StatusMessage;
import org.mcjug.locationmailer.util.EmailUtil;
import org.mcjug.locationmailer.util.ExceptionUtil;
import org.mcjug.locationmailer.util.LocationUtil;
import org.mcjug.locationmailer.util.NetworkConnectivity;
import org.mcjug.locationmailer.util.RunnableWithException;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;

public class AsyncLocationServiceTask extends AsyncTask<Void, Void, Void> {

    private StatusMessage statusMessage;

    private Location currentLocation;

    private Context context;

    private StatusMessageDatabaseHelper statusMessageHelper;

    private LocationService locationService;

    public AsyncLocationServiceTask(StatusMessage statusMessage, Location currentLocation, Context context,
            StatusMessageDatabaseHelper statusMessageHelper, LocationService locationService) {
        super();
        this.statusMessage = statusMessage;
        this.currentLocation = currentLocation;
        this.context = context;
        this.statusMessageHelper = statusMessageHelper;
        this.locationService = locationService;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            statusMessage = getStatusMessage(currentLocation);
            if (currentLocation == null) {
                statusMessage.setMessage(context.getString(R.string.msgNoLocation));
            }

            if (NetworkConnectivity.isAvailable(context)) {
                EmailUtil.sendEmail(statusMessage, context);
            } else {
                statusMessage.setMessage(context.getString(R.string.msgNoConnectivity));
            }

            // Store status message in database
            statusMessageHelper.insertOrIgnore(statusMessage);

            // Broadcast location to activity to show status message in location list
            context.sendBroadcast(new Intent(LocationService.NEW_LOCATION_INTENT_ACTION),
                    LocationService.RECEIVE_LOCATION_NOTIFICATIONS);
        } catch (Throwable e) {
            // Log.e(TAG, "LocationService asyncMessageWork AsyncTask doInBackground error", e);
            ExceptionUtil.exceptAway(context, e, "LocationService asyncMessageWork AsyncTask doInBackground error:",
                    new RunnableWithException() {

                        @Override
                        public void run() throws Throwable {
                            // Log.d(TAG,"About to clean up after LocationService asyncMessageWork AsyncTask doInBackground error");
                            locationService.stopSelf();
                            // Log.d(TAG,"Done cleaning up after LocationService asyncMessageWork AsyncTask doInBackground error");
                        }
                    });
        }

        return null;
    }

    // Location info to store in database
    private StatusMessage getStatusMessage(Location location) throws Throwable {
        statusMessage = new StatusMessage();
        statusMessage.setCreatedDate(new Date());
        statusMessage.setAddress(LocationUtil.getAddress(location, context));
        statusMessage.setLocation(location);
        statusMessage.setMessage("");
        return statusMessage;
    }
}
